package com.geektime.hello;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * @Author: DRevelation
 * @Date: 2021/1/25 15:55
 */
public class JacksonDemo {

    public static void main(String[] args) throws JsonProcessingException {
        String jsonStr = "{\"persons\":[{\"name\":\"a\",\"age\":11},{\"name\":\"b\",\"age\":12}]}";
        ObjectMapper mapper = new ObjectMapper();
        JsonNode node = mapper.readTree(jsonStr);
        JsonNode name = node.get("persons").get(0).get("name");
        System.out.println("name = " + name);
        JsonNode persons = node.get("persons");
        for (JsonNode person : persons) {
            System.out.println("age = " + person.get("age"));
        }
    }
}
